package com.rt.generalframework.core.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class WebServiceAspect {

    //申明一个切点 里面是 execution表达式
    @Pointcut("execution(public * com.rt.generalframework.ws.WebServiceImpl.*(..))")
    private void webServiceAspect(){
    }

    @Around("webServiceAspect()")
    public Object methodAround(ProceedingJoinPoint joinPoint) {
        //开始时间
        long startTimeMillis = System.currentTimeMillis();
        //获取请求的方法
        String methodName = joinPoint.getSignature().getName();
        //获取请求参数
        Object[] args = joinPoint.getArgs();
        log.info("【" + methodName + "】请求内容：{}", args.length > 0 ? args[0].toString() : "null");
        Object result = null;
        try {
            //执行被切的函数，result为被切目标函数返回的内容
            result = joinPoint.proceed();
            log.info("【" + methodName + "】响应内容：{}", result);
            log.info("【" + methodName + "】响应耗时：{}毫秒", System.currentTimeMillis() - startTimeMillis);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;
    }
}
